// 1 使用new Object()关键字创建对象
let object = new Object()
object.name = 'tom'
object.age = '20'
object.gender = '男'
object.show = function () {
  console.log(this) // object对象
  // 普通函数中this是函数的当前调用者
  // 箭头函数中没有自己的this，是当前函数被调用时所在作用域
}
object.show()

// 2 直接字面量创建对象
let person = {
  id: 1,
  name: 'tom',
  age: 25,
  gender: '男',
  show() {
    console.log(this)
  },
}
person.show()

// 3 使用工厂函数创建对象
function createObject() {
  let object = new Object()
  object.name = 'elon'
  object.gender = '女'
  object.age = 20
  object.show = function () {
    console.log(`{ name: ${this.name}, age: ${this.age}, gender: ${this.gender} }`)
  }
  return object
}
let user1 = createObject()
let user2 = createObject()
console.log(user1)
console.log(user2)
console.log(user1 === user2) // false,对象是否相等判断是数类型和内存地址是否同时相等

// 4 构造函数创建对象
function Dog(name, gender, strain, age) {
  this.age = age || 2
  this.name = name || '旺财'
  this.strain = strain || '中华田园犬'
  this.gender = gender || '公'
  this.show = function () {
    console.log(`{ name: ${this.name}, age: ${this.age}, gender: ${this.gender}, strain: ${this.strain} }`)
  }
}
let dog = new Dog('旺旺', '母', '泰迪', 1) // { name: 旺旺, age: 1, gender: 母, strain: 泰迪 }
dog.show() // { name: 旺财, age: 20, gender: 公, strain: 中华田园犬 }

// 5 使用ES6语法class创建对象
class Person {
  // 构造函数
  constructor(id, name, age, gender) {
    this.id = id || 1
    this.name = name || '狗娃'
    this.age = age || 25
    this.gender = gender || '男'
  }
  // show方法不是this实例的方法或属性，是this原型对象的方法
  show() {
    console.log(`{ name: ${this.name}, age: ${this.age}, gender: ${this.gender}, id: ${this.id} }`)
  }
}
let p = new Person(25, 'sam', 20, '女')
p.show() // { name: sam, age: 20, gender: 女, id: 25 }
console.log(typeof Person) // function
